<?php

namespace app\api\controller;

use app\BaseController;

class Student extends BaseController
{


    /**
     * 2025 03 04
     * Administrator
     * @return \think\response\Json
     */
    public function student()
    {

        $list_rows = $this->request->param('list_rows', '10', 'trim');
        $page = $this->request->param('page', '1', 'trim');
        $classes_id = $this->request->param('classes_id', '', 'trim');
        $today_sign_in = $this->request->param('today_sign_in', '', 'trim');

        $data = [
            'classes_id' => $classes_id,
            'list_rows' => $list_rows,
            'page' => $page,
            'today_sign_in' => $today_sign_in,
        ];
        $data = array_filter($data);

        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->student($data);
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 2025 04 09
     * Administrator
     * @return void
     */
    public function student_one(){
        $id = $this->request->param('id', '10', 'trim');

        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->student_one($id);
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }

    }


//    管理员登陆
    public function login(){
        $name = $this->request->param('name', '', 'trim');
        $password = $this->request->param('password', '', 'trim');
        $data = [
            'name' => $name,
            'password' => $password,
        ];
        //        TP6检验机制
//        try {
//            validate(\app\student\validate\Admin::class)->scene('name')->check($data);
//        } catch (\think\exception\ValidateException $e) {
//            return show(config('status.error'), $e->getError());
//        }
        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->login($data);
            return show(config('common.http_status.success'), '登录成功',$result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }

    //    退出登录
    public function logout(){
        $id = ($this->request->userInfo)['id'];
        $token = $this->request->token;
        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->logout($id,$token);
            return show(config('common.http_status.success'), '退出登录',$result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }

    //    修改用户密码
    public function updatePassword(){
        $id = ($this->request->userInfo)['id'];
        $password = $this->request->param('password', '', 'trim');

        if (empty($password)) {
            return show(config('status.error'), '密码为空', []);
        }

        $data = [
            'password' => $password,
            'id' => $id,
        ];

        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->updatePassword($data);
            return show(config('common.http_status.success'), 'ok',$result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }

    //    获取用户信息
    public function get_info(){
        $id = ($this->request->userInfo)['id'];
        try {
            $studentObj = new \app\common\business\Student();
            $result = $studentObj->get_info($id);
            return show(config('common.http_status.success'), 'ok',$result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }


    /**
     * 2025 03 04
     * Administrator
     * @return \think\response\Json
     */
    public function classes()
    {

        try {
            $classesObj = new \app\common\business\Classes();
            $result = $classesObj->classes();
            return show(config('common.http_status.success'), 'ok', $result);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
    }

}